package com.zds.support.util


import android.app.Activity
import android.content.Context
import android.os.Build
import android.view.View
import android.view.ViewGroup
import android.view.Window
import androidx.annotation.NonNull


object NavigationBarUtils {

    /**
     * <P>shang</P>
     * <P>判断是否有虚拟按键</P>
     * @param context
     * @return
     */
    fun checkDeviceHasNavigationBar(context: Context): Boolean {
        var hasNavigationBar = false
        val rs = context.getResources()
        val id = rs.getIdentifier("config_showNavigationBar", "bool", "android")
        if (id > 0) {
            hasNavigationBar = rs.getBoolean(id)
        }
        try {
            val systemPropertiesClass = Class.forName("android.os.SystemProperties")
            val m = systemPropertiesClass.getMethod("get", String::class.java)
            val navBarOverride = m.invoke(systemPropertiesClass, "qemu.hw.mainkeys") as String
            if ("1" == navBarOverride) {
                hasNavigationBar = false
            } else if ("0" == navBarOverride) {
                hasNavigationBar = true
            }
        } catch (e: Exception) {
        }

        return hasNavigationBar
    }

    /**
     * 隐藏虚拟按键，并且设置成全屏
     */
    fun hideNavigationBar(window: Window) {
        if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            val decorView = window.getDecorView()
            val flags = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    or View.SYSTEM_UI_FLAG_FULLSCREEN
                    or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
            window.getDecorView().setSystemUiVisibility(flags)
            window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
                if (visibility and View.SYSTEM_UI_FLAG_FULLSCREEN == 0) {
                    decorView.systemUiVisibility = flags
                }
            }
        }
    }

    /* */
    /**
     * <P>shang</P>
     * <P>解决虚拟按键问题</P>
     * @param window
     *//*
    fun hideNavigationBar(window: Window) {

            //控制底部虚拟键盘
        window.getDecorView().setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar

                 //                        | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                                        or View.SYSTEM_UI_FLAG_IMMERSIVE)
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
    }*/

    private val NAVIGATION: String? = "navigationBarBackground"

    // 该方法需要在View完全被绘制出来之后调用，否则判断不了
    //在比如 onWindowFocusChanged（）方法中可以得到正确的结果
    //在比如 onWindowFocusChanged（）方法中可以得到正确的结果
    fun isNavigationBarExist(@NonNull activity: Activity): Boolean {
        val vp: ViewGroup = activity.window.decorView as ViewGroup
        for (i in 0 until vp.childCount) {
            vp.getChildAt(i).context.packageName
            if (vp.getChildAt(i).id !== View.NO_ID && NAVIGATION == activity.resources.getResourceEntryName(vp.getChildAt(i).id)) {
                return true
            }
        }
        return false
    }
}